<template>
	<view class="homeLayout pageBg">
		<custom-nav-bar title="首页"></custom-nav-bar>
		<view class="banner">
			<swiper circular indicator-dots indicator-color="rgba(255,255,255,0.5)" indicator-active-color="#fff"
				autoplay>
				<template v-if="bannerList.length === 0">
					<swiper-item>
						<image src="/static/images/banners/banner1.jpg" mode="aspectFill"></image>
					</swiper-item>
				</template>
				<template v-else>
					<swiper-item v-for="item in bannerList" :key="item.id">
						<image :src="item.image" mode="aspectFill"></image>
					</swiper-item>
				</template>
			</swiper>
		</view>

		<view class="notice">
			<view class="left">
				<uni-icons type="sound-filled" size="20"></uni-icons>
				<text class="text">公告</text>
			</view>
			<view class="center">
				<swiper vertical autoplay interval="1500" duration="300" circular>
					<swiper-item v-for="item in noticeList" :key="item.id">
						<navigator :url="'/pages/notice/notice?id=' + item.id">
							{{ item.title }}
						</navigator>
					</swiper-item>
				</swiper>
			</view>
			<view class="right">
				<uni-icons type="right" size="16" color="#333"></uni-icons>
			</view>
		</view>

		<view class="select">
			<common-title>
				<template #name>快捷预约</template>
				<template #custom>
					<view class="date">
						<uni-icons type="calendar" size="18"></uni-icons>
						<view class="text">
							<uni-dateformat :date="Date.now()" format="dd日"></uni-dateformat>
						</view>
					</view>
				</template>
			</common-title>
			<view class="content">
				<scroll-view scroll-x>

					<view class="item" v-for="item in enableDepartment" :key="item.id" @click="goReservation(item.id)">
						<view class="pic">
							<image :src=item.image mode="aspectFill" />
						</view>
						<view class="text">
							{{ item.name }}
						</view>
					</view>
				</scroll-view>
			</view>
		</view>
	</view>
</template>

<script setup>
import {
	getEnableDepartment, getBannerList, getAnnouncementList
} from '@/api/apis.js'
import {
	ref,
	onMounted
} from 'vue'
import {
	useUserStore
} from '@/stores/index.js';

const userStore = useUserStore();

// 获取轮播图
const bannerList = ref([])

// 获取公告
const noticeList = ref([])

// 获取部门
const enableDepartment = ref([])

onMounted(async () => {
	
	await getBannerList().then(res => {
		// console.log(res);
		bannerList.value = res.data
	})
	await getEnableDepartment().then(res => {
		// console.log(res);
		enableDepartment.value = res.data
	})
	await getAnnouncementList().then(res => {
		console.log(res);
		noticeList.value = res.data
	})

	// console.log("ws");
	// ws();
	
	// if(getApp().verifyLogin()){
	// 	console.log('已登陆')
	// }

})

// const ws = () => {
// 	uni.connectSocket({
// 		url: 'ws://localhost:9090/ws/' + userStore.user.id,
// 		success: (res) => {
// 			console.log("连接成功", res);
// 		},
// 		fail: (err) => {
// 			console.log("连接失败", err);
// 		}
// 	});
// 	uni.onSocketMessage(function (res) {
// 		console.log('收到服务器内容：' + res);
// 		uni.showModal({
// 			title: "消息提示",
// 			content: res.data,
// 			showCancel: false,
// 		})
// 	});


// }

const goReservation = (id) => {
	// console.log(id);
	uni.reLaunch({
		url: "/pages/reservation/reservation?departmentId=" + id
	})
}
</script>

<style lang="scss" scoped>
.homeLayout {
	.banner {
		width: 750rpx;
		padding: 30rpx 0;

		swiper {
			width: 750rpx;
			height: 340rpx;

			&-item {
				width: 100%;
				height: 100%;
				padding: 0 30rpx;

				image {
					width: 100%;
					height: 100%;
					border-radius: 10rpx;
				}
			}
		}
	}

	.notice {
		width: 690rpx;
		height: 80rpx;
		line-height: 80rpx;
		background: #f9f9f9;
		margin: 0 auto;
		border-radius: 80rpx;
		display: flex;

		.left {
			width: 140rpx;
			display: flex;
			align-items: center;
			justify-content: center;

			:deep() {
				.uni-icons {
					color: $brand-theme-color !important;
				}
			}

			.text {
				color: $brand-theme-color;
				font-weight: 600;
				font-size: 28rpx;
			}
		}

		.center {
			flex: 1;

			swiper {
				height: 100%;

				&-item {
					height: 100%;
					font-size: 30rpx;
					color: #666;
					overflow: hidden;
					white-space: nowrap;
					text-overflow: ellipsis;
				}
			}
		}

		.right {
			width: 70rpx;
			display: flex;
			align-items: center;
			justify-content: center;
		}
	}

	.select {
		padding-top: 50rpx;

		.date {
			color: $brand-theme-color;
			display: flex;
			align-items: center;

			:deep() {
				.uni-icons {
					color: $brand-theme-color !important;
				}
			}

			.text {
				margin-left: 5rpx;
			}
		}

		.content {
			padding: 60rpx 30rpx;

			scroll-view {
				white-space: nowrap;

				.item {
					display: inline-block;
					padding: 0 25rpx;
					text-align: center;

					.pic {
						width: 105rpx;
						height: 105rpx;

						image {
							width: 100%;
							height: 100%;
							border-radius: 10rpx;
						}
					}

					.text {
						font-size: 28rpx;
						color: var(--globalColor);
						padding-top: 10rpx;
					}
				}

				.item:first-child {
					padding-left: 0;
				}

				.item:last-child {
					padding-left: 0;
				}
			}
		}
	}

	.theme {
		padding: 50rpx 0;

		.more {
			font-size: 32rpx;
			color: #888;

		}

		.content {
			margin-top: 30rpx;
			padding: 0 30rpx;
			display: grid;
			gap: 15rpx;
			grid-template-columns: repeat(3, 1fr);
		}
	}
}
</style>